#include "stdio.h"
#include "rocc.h"
#include "encoding.h"
#include "util.h"
// #include "mmio.h"
#define MAXSIZE 256
#define N_IN 576
#define N_OUT 128

union __attribute__((aligned(8))) Data
{
    uint64_t data64[N_IN/8];
    int8_t   data8 [N_IN];
};

union __attribute__((aligned(8))) Result
{
    uint64_t data64[N_OUT/8];
    int8_t   data8 [N_OUT];
};

int main(void){
    //int8_t data[576]    = {0};
    union Data   data  ;
    //union Result result;
    int8_t result[128]  = {0};
    uint64_t push_rs2 , save_rs2,mvm_rs2,mvm_rs1 ,config;

    push_rs2 = 0  << 11   | (576);  //reg: row start, num
    save_rs2 = 96  << 11  | 16 ; //reg: col start, num
    mvm_rs2=   1  << 11   | 96 ;  //mvm: row_start, col_start
    mvm_rs1 =  1  << 16   | 10;  //mvm: row_num,   col_num
    config = 0<< 23 | 1<< 16| 0<<8 | 0;
    for (int j = 0; j < 576; j++){
	    if(j<128){
	    	result[j] = 9;
	    }
            data.data8[j] = 2;
    }

    long long tmp [72];

    for (int j = 0; j < 72; j++){
            //tmp[j] = 0x1232020202020202 + j ;
            tmp[j] = 0x0202020202020202 ;
    }

        printf("%ld %ld\n",sizeof(data),sizeof(result));


    // 1. config 2
    // random_or_not| wise_num| out_shift|in_shift

    asm volatile("fence");
    ROCC_INSTRUCTION_SS(0,config,0,2);
        printf("finish config\n");
    asm volatile("fence":::"memory");

    // 2. push 17
    asm volatile("fence");
   // ROCC_INSTRUCTION_SS(0,(uint64_t) data.data64,push_rs2,17);
    ROCC_INSTRUCTION_SS(0,tmp,push_rs2,17);
printf("finish push\n");
    asm volatile("fence":::"memory");

    // 3. mvm  1
    asm volatile("fence");
    ROCC_INSTRUCTION_SS(0,mvm_rs1,mvm_rs2,1);
printf("finish mvm\n");
    asm volatile("fence":::"memory");

    // 4. save 18
    asm volatile("fence");
    //ROCC_INSTRUCTION_SS(0,(uint64_t)result.data64,save_rs2,18);
    ROCC_INSTRUCTION_SS(0,(uint64_t)result,save_rs2,18);
printf("finish save\n");
    asm volatile("fence":::"memory");


    for (int j = 0; j < 128; j++){
        printf("%4d ",result[j]) ;
        if((j+1)%10==0) printf("\n");
    }

printf("\nfinish all\n");

    return 0;
}
